# -*- coding: UTF-8 -*-
__author__ = 'kakake'
global service
service.printlog('begin load BookDemoController2.py')

import sys
sys.path.append("ModulePlugin/BooksService")
#将trace设置为跨文件的全局变量
import controller.globalservice as glo
glo.set_service(service)

#from entity.Book import Book


import json
from datetime import datetime


#入口函数
def main(servicecode):
  try:
    service.printlog('call main')
    bookController= BookDemoController2()
    eval("bookController."+servicecode+"()")
    return 0
  except Exception,ex:
    service.printlog(ex.message)
    return ex.message;



#控制器类
##############################################################
class BookDemoController2(object):
  def __init__(self):
    pass
  def service001(self):
    #return "hello world"
    service.printlog("hello")
    service.responsejson("hello world")

  def GetBooks(self):
    sql="select * from BOOKS"
    #service.printlog(sql)
    data= service.dbquery(sql)
    service.printlog(data)
    service.responsejson(data)

  def SaveBook(self):
    para=json.loads(service.requestjson(1))
    sql=''
    if int(para['Id'])==0:
      sql_insert="insert into books(BookName,BuyPrice,BuyDate,Flag) values('%s',%s,'%s',%s)"
      sql=sql_insert % (para['BookName'],str(para['BuyPrice']),para['BuyDate'],str(para['Flag']))
    else:
      sql_update="update books set BookName='%s',BuyPrice=%s,BuyDate='%s',Flag=%s where Id=%s"
      sql=sql_update % (para['BookName'],str(para['BuyPrice']),para['BuyDate'],str(para['Flag']),str(para['Id']))
    #service.printlog(sql)
    service.dbnonquery(sql)
    service.responsejson("true")

  def DeleteBook(self):
    sql="delete from books where Id=%s" % int(service.requestjson(1))
    #service.printlog(sql)
    service.dbnonquery(sql)
    service.responsejson("true")

##############################################################



#日志显示脚本加载完成
service.printlog('end load BookDemoController2.py')